package com.shopcentre15.shopcentre.FormalShopExplot.Controller.Consumer;

import com.shopcentre15.shopcentre.FormalShopExplot.Entity.ShopConsumer;
import com.shopcentre15.shopcentre.FormalShopExplot.Service.Consumer.ConsumerService;
import com.shopcentre15.shopcentre.FormalShopExplot.Utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumer_func")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @PostMapping("/register")
    @ResponseBody
    public CommonResult register(@RequestBody ShopConsumer shopConsumer){
        try{
            String account = shopConsumer.getAccount();
            String password = shopConsumer.getPassword();
            if(account == null || password == null){
                return CommonResult.fail(400,"请输入账号和密码");
            }
            Integer register = consumerService.register(shopConsumer);
            if(register > 0){
                return CommonResult.success();
            }
            return CommonResult.fail(500,"注册失败");
        }catch (RuntimeException e ){
            e.printStackTrace();
            return CommonResult.fail(500,"注册失败");
        }
    }

    @PostMapping("/login")
    @ResponseBody
    public CommonResult login(@RequestBody ShopConsumer shopConsumer){
        try{
            String account = shopConsumer.getAccount();
            String password = shopConsumer.getPassword();
            if(account == null || password == null){
                return CommonResult.fail(400,"请输入账号或密码");
            }
            ShopConsumer login = consumerService.login(shopConsumer);
            if(login == null){
                return CommonResult.fail(500,"账号或密码错误");
            }
            return CommonResult.success(login,null,null);
        }catch (RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500,"登录失败");
        }
    }
    @PostMapping("/updateConsumer")
    @ResponseBody
    public CommonResult updateConsumer(@RequestBody ShopConsumer shopConsumer){
        try{
            String account = shopConsumer.getAccount();
            if(account == null){
                return CommonResult.fail(400,"请先登录");
            }
            Integer update = consumerService.updateConsumer(shopConsumer);
            if(update == 0){
                return CommonResult.fail(500,"更新失败");
            }
            return CommonResult.success();
        }catch (RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500,"更新失败");
        }
    }
}
